package com.iflysse.servlet.login;

import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import com.iflysse.pojo.MenuInfo;
import com.iflysse.pojo.Node;
import com.iflysse.pojo.UserInfo;
import com.iflysse.service.UserInfoService;
import com.iflysse.utils.Md5;

/**
 * Servlet implementation class LoginHandlerServlet
 */
/**
 * @author wuyigang
 *
 */
@WebServlet("/login/LoginHandlerServlet.do")
public class LoginHandlerServlet extends HttpServlet {
	static Logger logger = Logger.getLogger(LoginHandlerServlet.class);
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginHandlerServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String str = this.getServletContext().getRealPath("/");
		String path = str + "log4j.properties";
		PropertyConfigurator.configure(path);
		logger.error("hanludjfalkfdj ");
		UserInfoService service = new UserInfoService();
		String name = request.getParameter("name");
		String passwd = Md5.mD5(request.getParameter("passwd"));// 使用md5对用户输入的密码进行加密,然后再与数据库中的已经加密的数据进行比较
//		System.out.println(passwd);
		Writer writer = response.getWriter();

		List<UserInfo> infoList = service.findUserByName(name);
		if (infoList.size() == 0) {
			writer.write("系统中不存在此用户");
			writer.flush();
			return;
		}
		UserInfo info = infoList.get(0);
		// System.out.println(info);
		if (info == null) {
			writer.write("系统中不存在此用户");
			writer.flush();
			return;
		}

		if (!(info.getPsw().equals(passwd))) {
			writer.write("此用户密码不对");
			writer.flush();
			return;
		}

		// roleIdList为该用户所有的角色id
		List<Integer> roleIdList = service.findRoleIdListByUserId(info
				.getUserid());
		// System.out.println(roleIdList);
		Set<Integer> menuIdSet = new HashSet<Integer>();
		for (int i = 0; i < roleIdList.size(); i++) {
			List<Integer> tempList = service.findMenuIdListByRoleId(roleIdList
					.get(i));
			for (Integer integer : tempList) {
				menuIdSet.add(integer);
			}
		}
//		System.out.println("所有的菜单集合为：");
//		System.out.println(menuIdSet);

		// 找到所有的父菜单Id
		Set<Integer> parentIdSet = new HashSet<Integer>();
		List<MenuInfo> menuInfos = new ArrayList<MenuInfo>();
		for (Iterator iterator = menuIdSet.iterator(); iterator.hasNext();) {
			Integer currId = (Integer) iterator.next();
			Integer parentId = service.getMenuParentIdByMenuId(currId);
			menuInfos.add(service.getMenuInoByMenuId(currId));
			parentIdSet.add(parentId);
		}

		// System.out.println(parentIdSet);// 打印所有父菜单Id [2, 3, 4, 5, 6]

		// System.out.println("----------------------");
		// System.out.println(menuInfos);
		// System.out.println("----------------------");
		Integer[] parentIdArray = parentIdSet.toArray(new Integer[0]);
		List<Node> menuNode = new ArrayList<Node>();

		for (int i = 0; i < parentIdArray.length; i++) {
			Node node = new Node();
			MenuInfo menuInfo = service.getMenuInoByMenuId(parentIdArray[i]);
			node.setMenuIno(menuInfo);
			for (int j = 0; j < menuInfos.size(); j++) {
				if (menuInfos.get(j).getParentId() == parentIdArray[i]) {
					node.getList().add(menuInfos.get(j));
				}
			}
			menuNode.add(node);
		}
		// System.out.println(menuNode);
		HttpSession session = request.getSession();
		session.setAttribute("user", info);
		session.setAttribute("menu", menuNode);

	}

}
